package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Contract;
import com.woniuxy.oldmanapi.entity.Life;
import com.woniuxy.oldmanapi.service.LifeService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.yvo.ContractVo;
import com.woniuxy.oldmanapi.yvo.LifeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class LifeController {
    @Autowired
    LifeService lifeService;
    @GetMapping("/life/list")
    public ResponseResult<PageInfo<LifeVo>> initSelectAllByCons(String name, int now, int size) {
        ResponseResult<PageInfo<LifeVo>> result = new ResponseResult<>();
        try {
            PageInfo<LifeVo> pageInfo = lifeService.initSelectAllByCons(name, now, size);
            if (pageInfo.getList() == null) {
                result.setCode(204);
                result.setMsg("无满足条件对象");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @DeleteMapping("/life/deleteLife/{id}")
    public ResponseResult<Void> deleteLife(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            lifeService.deleteLife(id);
            result.setMsg("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }


    @PostMapping("/life/addLife")
    public ResponseResult<Void> addLife(@RequestBody Life life) {
        /*测试*/
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            lifeService.addContract(life);
            result.setMsg("创建成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @GetMapping("/life/selectLifeById/{id}")
    public ResponseResult<LifeVo> selectLifeById(@PathVariable("id") int id) {
        ResponseResult<LifeVo> result = new ResponseResult<>();
        try {
            LifeVo lifeVo = lifeService.selectLifeById(id);
            if (lifeVo == null) {
                result.setCode(202);
                result.setMsg("此对象信息有误,请稍后再试");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(lifeVo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PutMapping("/life/modifyLife")
    public ResponseResult<Void> modifyLife(@RequestBody Life life) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            lifeService.modifyLife(life);
            result.setMsg("修改成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
}
